Skip to content

fix(schedulers): add set_begin_index to PNDMScheduler for img2img timestep alignment - #15431

Open
shravansumanthanan wants to merge 1 commit into
huggingface:mainfrom
shravansumanthanan:fix/pndm-set-begin-index
Open

shravansumanthanan wants to merge 1 commit into
huggingface:mainfrom
shravansumanthanan:fix/pndm-set-begin-index

Conversation

@shravansumanthanan

Copy link
Copy Markdown

What does this PR do?

Closes #15429.

PNDMScheduler was the only common scheduler missing the set_begin_index / begin_index interface. Every img2img and inpaint pipeline already calls set_begin_index via:

if hasattr(self.scheduler, "set_begin_index"):
    self.scheduler.set_begin_index(t_start * self.scheduler.order)

Because PNDMScheduler lacked the method, the hasattr guard silently skipped the call. The denoising loop then started from scheduler.timesteps[t_start:] with counter = 0, so the PLMS corrector branch (counter == 1) fired at the wrong denoising step — one slot off — producing subtly incorrect samples for all img2img runs with PNDMScheduler.

Root cause

step_plms has two critical counter-based branches:

condition effect
counter != 1 accumulate new ets entry (predictor)
counter == 1 average with previous ets entry (corrector), re-use saved sample

When t_start > 0 and counter is never offset, the corrector fires at the second global call instead of the second call within the sub-loop, shifting all subsequent multi-step estimates.

Fix

  1. Add _begin_index = None in __init__ and reset it in set_timesteps (matching DPMSolverMultistepScheduler).
  2. Add begin_index property + set_begin_index() method (copied from DPMSolverMultistepScheduler, the canonical source).
  3. In step_plms, compute effective_counter = counter - begin_index (falls back to counter when begin_index is None/0) and use it for all counter-relative comparisons.

No behaviour change when set_begin_index is never called (default begin_index = None, effective_counter == counter).

Who can review?

@sayakpaul @DN6 @yiyixuxu

Checklist

…estep alignment

PNDMScheduler was missing the set_begin_index / begin_index interface that
img2img pipelines call when they skip the first t_start timesteps
(scheduler.timesteps[t_start:]). Without it, hasattr(scheduler,
'set_begin_index') returns False, so pipelines silently skip the call, and
the internal counter stays at 0 while the denoising loop starts mid-schedule.

This causes step_plms to fire the PLMS corrector branch (counter == 1, the
two-step average) at the wrong position, shifting all subsequent multi-step
estimates by one slot and producing subtly wrong outputs.

Fix:
- Add _begin_index = None to __init__ and to set_timesteps (reset on each
  new run, matching DPMSolverMultistepScheduler behaviour).
- Add begin_index property and set_begin_index() method, copied from
  DPMSolverMultistepScheduler (the canonical source for this interface).
- In step_plms, compute effective_counter = counter - begin_index (falls
  back to counter when begin_index is None / 0) and use it for all
  counter-relative checks (counter != 1, counter == 0, counter == 1).

Closes huggingface#15429

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PNDMScheduler mislabels timesteps when img2img starts part-way through the schedule

1 participant